/* 演示 promise<int> 在线程间传递结果

*/

#include <chrono>
#include <future>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
using namespace std;

void add_string_value(string a, string b,
    std::promise<string> promise_str)
{
    string sum = a + b;
    cout << "add_string_value start" << endl;
    promise_str.set_value(sum); // 提醒 future
    cout << "add_string_value end" << endl;
}

void add_string_ref(string& a, string& b,
    std::promise<string> promise_str)
{
    string sum = a + b;
    cout << "add_string_ref start" << endl;
    promise_str.set_value(sum); // 提醒 future
    cout << "add_string_ref end" << endl;
}

int main()
{
    promise<string> promise_str;
    future<string> future_str = promise_str.get_future();
    string a = "123", b = "abc";
    thread t1(add_string_ref, ref(a), ref(b), move(promise_str));
    cout << "ref result : " << future_str.get() << endl;

    promise<string> promise_str2;
    // future<string> future_str2 = promise_str2.get_future();
    future_str = promise_str2.get_future(); //运行ok: future 可以绑定多个
    thread t2(add_string_value, a, b, move(promise_str2));
    cout << "value result : " << future_str.get() << endl;
    t1.join();
    t2.join();

    return 0;
}